package me.yuxiaoyao.lock.interceptor;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.util.Assert;

/**
 * @author kerryzhang on 2020/12/21
 */


public class DistributedLockAnnotateInterceptor extends DistributedLockAspectSupport implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {

        Invoker invoker = () -> {
            try {
                return methodInvocation.proceed();
            } catch (Throwable throwable) {
                throw new ThrowableWrapper(throwable);
            }
        };
        Object target = methodInvocation.getThis();
        Assert.state(target != null, "Target must not be null");

        try {
            // 执行
            return execute(invoker, target, methodInvocation.getMethod(), methodInvocation.getArguments());
        } catch (ThrowableWrapper tw) {
            throw tw.getOrigin();
        }
    }
}
